package com.skyline.wo.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.skyline.wo.model.Article;

/**
 * ArticleSimpleMapper mapping the record to Article. This Mapper wouldn't map
 * the content to Article.So it could be used for querying Articles in a list.
 * 
 * @author Jairus Chan
 * @version 0.1
 */
public class ArticleSimpleMapper implements RowMapper<Article> {

	private static final ArticleSimpleMapper MAPPER = new ArticleSimpleMapper();

	private static final String COLUMN_ID = "id";
	private static final String COLUMN_DIGEST = "digest";
	private static final String COLUMN_TITLE = "title";
	private static final String COLUMN_OWNER_ID = "ownerId";
	private static final String COLUMN_OWNER_NICKNAME = "ownerNickname";
	private static final String COLUMN_OWNER_PORTRAIT = "ownerPortrait";
	private static final String COLUMN_CATEGORY_ID = "categoryId";
	private static final String COLUMN_CATEGORY_NAME = "categoryName";
	private static final String COLUMN_COMMENT_NUM = "commentNum";
	private static final String COLUMN_VISIT_NUM = "visitNum";
	private static final String COLUMN_UP = "up";
	private static final String COLUMN_DOWN = "down";
	private static final String COLUMN_SHARE_NUM = "shareNum";
	private static final String COLUMN_CREATE_TIME = "createTime";
	private static final String COLUMN_UPATE_TIME = "updateTime";
	private static final String COLUMN_AUTHORITY = "authority";

	public static ArticleSimpleMapper getMapper() {
		return MAPPER;
	}

	public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
		Article article = new Article();
		article.setAuthority(rs.getInt(COLUMN_AUTHORITY));
		article.setCommentNum(rs.getInt(COLUMN_COMMENT_NUM));
		article.setCategoryId(rs.getLong(COLUMN_CATEGORY_ID));
		article.setCategoryName(rs.getString(COLUMN_CATEGORY_NAME));
		article.setDigest(rs.getString(COLUMN_DIGEST));
		article.setCreateTime(rs.getTimestamp(COLUMN_CREATE_TIME));
		article.setId(rs.getLong(COLUMN_ID));
		article.setOwnerId(rs.getLong(COLUMN_OWNER_ID));
		article.setOwnerNickname(rs.getString(COLUMN_OWNER_NICKNAME));
		article.setOwnerPortrait(rs.getString(COLUMN_OWNER_PORTRAIT));
		article.setVisitNum(rs.getInt(COLUMN_VISIT_NUM));
		article.setShareNum(rs.getInt(COLUMN_SHARE_NUM));
		article.setTitle(rs.getString(COLUMN_TITLE));
		article.setUpdateTime(rs.getTimestamp(COLUMN_UPATE_TIME));
		article.setUp(rs.getInt(COLUMN_UP));
		article.setDown(rs.getInt(COLUMN_DOWN));
		return article;
	}
}
